package utils

import (
	"fmt"
	"github.com/astaxie/beego/logs"
	"gopkg.in/yaml.v2"
	"os"
	"strconv"
	"strings"
)

var (
	Lg  *logs.BeeLogger
	Con *Config
)

func init() {
	Lg = logs.NewLogger()
	_ = Lg.SetLogger(logs.AdapterConsole)
	_ = Lg.SetLogger(logs.AdapterFile,
		`{"filename":"service.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10}`)
	Lg.Info("日志初始化完成")
	initConfigFile()

}

//配置文件中的结构体
type Config struct {
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Islog    bool   `yaml:"islog"`
	CronStr  string `yaml:"cronStr"`
}

// 打开yaml的配置文件，并初始化 配置信息
func initConfigFile() {
	file, err := os.Open("config.yaml")
	if err != nil {
		panic(err)
	}
	decoder := yaml.NewDecoder(file)
	_ = decoder.Decode(&Con)
	Lg.Info("配置文件=%v", Con)
}

func GettextUnquoted(textUnquoted string) string {
	sUnicodev := strings.Split(textUnquoted, "\\u")
	var context string
	for _, v := range sUnicodev {
		if len(v) < 1 {
			continue
		}
		temp, err := strconv.ParseInt(v, 16, 32)
		if err != nil {
			panic(err)
		}
		context += fmt.Sprintf("%c", temp)
	}
	fmt.Println(context)
	return context
}
